//
//  CollectViewController.m
//  Blank
//
//  Created by 马博 on 15/9/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CollectViewController.h"
#import "DataBaseHandle.h"
#import "CollectCell.h"
#import "CollectModel.h"
#import "IssueViewController.h"
#import "Collect_Iss_ViewController.h"

#define windth [UIScreen mainScreen].bounds.size.width / 375
#define heightt ([UIScreen mainScreen].bounds.size.height > 568 ? [UIScreen mainScreen].bounds.size.height : 568) / 667

@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong) UITableView *tableView;


@end

@implementation CollectViewController


- (void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    
    [self dataHandle];
    
    if (self.collectArr.count == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你还没有收藏哦,赶紧去收藏吧!" preferredStyle:UIAlertControllerStyleAlert];
        [self.tableView removeFromSuperview];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"back" style:0 handler:^(UIAlertAction *action) {
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        
        [alert addAction:action];
    }
    else{
        [self.tableView reloadData];
    
    }
    

}

- (void)dataHandle
{
    self.collectArr = [NSMutableArray array];
    
    [[DataBaseHandle shareDataBaseHandle] open];
    
    self.collectArr = (NSMutableArray *)[[DataBaseHandle shareDataBaseHandle] selectCollect];
    
    [[DataBaseHandle shareDataBaseHandle] close];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self tableViewSubView];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonBack setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    buttonBack.frame = CGRectMake(0, 0, 20, 20);
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonBack];
    
    [buttonBack addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)back:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

- (void)tableViewSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(Windth * 0, 0, Windth * 375, Height * 667) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];

    [self.tableView registerClass:[CollectCell class] forCellReuseIdentifier:@"reuse"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.collectArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return Height * 60;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    CollectModel *model =[self.collectArr objectAtIndex:indexPath.row];

    if ([model.title isEqualToString:@"(null)"]) {

        cell.textLabel.text = @"aaaa";
    }
    else{
    cell.textLabel.text = model.title;
    }
    return cell;
}



/* 选中cell */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Collect_Iss_ViewController *issue = [[Collect_Iss_ViewController alloc] init];
    CollectModel *model = [self.collectArr objectAtIndex:indexPath.row];
    issue.url = model.url;
    issue.webTitle = model.title;
    
    [self.navigationController pushViewController:issue animated:YES];
}


/* 当点击 navigation 编辑按钮时，VC调用此方法 */
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    
    /* 首先调用父类的setEding 方法 */
    [super setEditing:editing animated:animated];
    
    /* 让tableView 进入或退出 编辑状态 */
    [self.tableView setEditing:editing animated:animated];
    
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* 不指定行数， 默认所有行数都可以编辑 */
    return YES;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

/* 让哪些cell可以进行编辑（UITableViewDataSource协议方法）*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        /* 编辑数据 */
        CollectModel *model = [[CollectModel alloc] init];
        model = [self.collectArr objectAtIndex:indexPath.row];
        
        /* 打开数据库 */
        [[DataBaseHandle shareDataBaseHandle] open];
        [[DataBaseHandle shareDataBaseHandle] deleteCollect:model.title];
        self.collectArr = (NSMutableArray *)[[DataBaseHandle shareDataBaseHandle] selectCollect];
        
        /* 关闭数据库 */
        [[DataBaseHandle shareDataBaseHandle] close];
        
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
